package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.Utils.Page;
import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Billoflading;
import com.example.shoppingmall.service.BillofladingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/billoflading")
public class BillofladingController {
    @Autowired
    BillofladingService billofladingService;

    @RequestMapping("/BillListAll")
    public String BillListAll(@RequestParam(value = "stroe",required = false) String stroe, @RequestParam(value = "status",required = false) String status,
                              @RequestParam(value = "time",required = false) String time, @RequestParam(value = "numberfordelivery",required = false) String numberfordelivery,
                              @RequestParam(value = "orderno",required = false) String orderno, @RequestParam(value = "bname",required = false) String bname,
                              @RequestParam(value = "bphone",required = false) String bphone, @RequestParam(value = "btypeId",required = false) String btypeId,
                              @RequestParam(value = "pageno",defaultValue = "1") String pageno, @RequestParam(value = "pageSize",defaultValue = "6") String pageSize, Model model){
        if(btypeId==null){
            btypeId="0";
        }
        model.addAttribute("numberfordelivery",numberfordelivery);
        model.addAttribute("orderno",orderno);
        model.addAttribute("bname",bname);
        model.addAttribute("bphone",bphone);
        int count=billofladingService.bcount(numberfordelivery,orderno,bname,bphone,btypeId);
        int num=(Integer.valueOf(pageno)-1)*(Integer.valueOf(pageSize));
        Page page=new Page();
        page.setPageno(Integer.valueOf(pageno));
        page.setPageSize(Integer.valueOf(pageSize));
        page.setPageCount(count);
        List<Billoflading> billofladings=billofladingService.getBillListAll(stroe, status, time, numberfordelivery, orderno, bname, bphone, btypeId, num, Integer.valueOf(pageSize));
        model.addAttribute("page",page);
        model.addAttribute("billofladings",billofladings);
        return "order/Billoflading";
    }

    //查看提货单详情
    @MyLog(value = "订单管理->提货单->查看提货详情")
    @RequestMapping("/selbill")
    public String selbill(Integer id,Model model){
        Billoflading billofladinglist=billofladingService.selbill(id);
        model.addAttribute("billofladinglist",billofladinglist);
        return "order/BillofladingSelected";
    }

    //弹窗显示提货订单信息
    @MyLog(value = "订单管理->提货单->显示提货信息")
    @RequestMapping("/selbillsuccess")
    @ResponseBody
    public Map<Object,Object> selbillsuccess(Integer id, HttpSession session){
        Map<Object,Object> map=new HashMap<>();
        Billoflading billofladinglist=billofladingService.selbill(id);
        map.put("billofladinglists",billofladinglist);
        session.setAttribute("billid",billofladinglist.getId());
        return map;
    }

    //提货完成操作
    @MyLog(value = "订单管理->提货单->提货完成")
    @RequestMapping("/billsuccess")
    public String billsuccess(HttpSession session){
        int id= (int) session.getAttribute("billid");
        if(billofladingService.billsuccess(id)>0){
            return "redirect:/billoflading/BillListAll";
        }
        return "error";
    }
}
